var express = require('express');
var router = express.Router();
let cookieSession = require('cookie-session');
let svgCaptcha = require('svg-captcha');
const { text } = require('body-parser');
// 获取post参数
router.use(express.urlencoded({
    extended: true
}));
/* GET home page. */
// 验证码
// 设置cookie钥匙
router.use(cookieSession({
    keys:[ 'abcdefg','gfhkjkffd']
}))
router.get('/yzm', (req, res) => {
    let { text, data } = svgCaptcha.create({
        // 验证码需要的东西
        noise: 8, size: 4, color: true, background: "#ccc", ignoreChars: "Il"
    })    // 储存验证码
    req.session.text = text;
    // 改变图片类型
    res.type('svg');
    // 渲染
    res.send(data)

})

// 渲染注册页面
router.get('/regester', (req, res) => {
    res.render('regester');
})
// 判断
router.post('/doregester', (req, res) => {
    let username = req.body.username;
    let password = req.body.password;
    // 储存用户账号密码
    req.session.username = username;
    req.session.password = password;
    //进行判空
    if (username == '' || password == '' || req.body.yzm == '') {
        res.send('请输入正确的用户名,密码,验证码');
        return;
    };
    // 判断验证码是否和session验证码一致
    if (req.session.text != req.body.yzm) {
        res.send('请输入正确的验证码');
        return;
    }
    res.send('注册成功')
})
module.exports = router;
